<%@page import="java.text.DecimalFormat"%>
<%@page import="java.text.DecimalFormatSymbols"%>
<%@page import="com.mconnect.msurvey.model.ReportCost"%>
<%@page import="com.mconnect.msurvey.controller.UserManager"%>
<%@page import="com.mconnect.msurvey.model.User"%>
<%@page import="com.mconnect.msurvey.controller.PlanManager"%>
<%@page import="com.mconnect.msurvey.model.Report"%>
<%@page import="com.mconnect.msurvey.model.PriceListRecommend"%>
<%@page import="com.mconnect.msurvey.model.PriceListSurvey"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.mconnect.msurvey.model.PriceList"%>
<%@page import="java.util.List"%>
<%@page import="com.mconnect.msurvey.controller.PriceListManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
		body { font-size: 62.5%; }
		label, input { display:block; }
		input.text { margin-bottom:12px; width:95%; padding: .4em; }
		fieldset { padding:0; border:0; margin-top:25px; }
		h1 { font-size: 1.2em; margin: .6em 0; }
		div#users-contain { width: 350px; margin: 20px 0; }
		div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
		div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
		b {color: red;}
	</style>
</head>
<body>
	<% int id_plan = Integer.parseInt(request.getParameter("id_plan")); 
		PriceListManager priceController = new PriceListManager();
		List<PriceList> priceList = new ArrayList<PriceList>();
		priceList = priceController.getPriceListById_plan(id_plan);
		List<PriceListSurvey> priceSurvey = priceList.iterator().next().getResponderPriceList();
		List<PriceListRecommend> priceRecommend = priceList.iterator().next().getRecommendPriceList();
		String dateFrom = request.getParameter("dateFrom");
    	String dateTo = request.getParameter("dateTo");
		PlanManager pM = new PlanManager();
		Report report = pM.getReportByIdPlan(id_plan, dateFrom, dateTo);
		
		List<User> userList = new ArrayList<User>();
		UserManager uM = new UserManager();
		userList = uM.getUserById_plan(id_plan);
		
		double totalSurveyCost = 0.0, totalRecommendCost = 0.0, totalCost = 0.0;
		DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
        symbols.setDecimalSeparator('.');
        DecimalFormat df = new DecimalFormat("0.00", symbols);
		
	%>
	<div id="users-contain" class="ui-widget" style="width: 850px; ">
		<div style="width: 460px; display: inline-block; max-height: 225px; overflow: auto; " >
			<table id="singleCost" class="ui-widget ui-widget-content" style="width: 420px; ">
				<thead>
					<tr class="ui-widget-header ">
						<th colspan="6">Należności dla ankieterów</th>
					</tr>
					<tr class="ui-widget-header ">
						<th>Ankieter</th>
						<th>Ankiet</th>
						<th>Należność</th>
						<th>Poleceń</th>
						<th>Należność</th>
						<th>Razem</th>
					</tr>
				</thead>
				<tbody>
					<%
						for(User user : userList){ 
									Report reportUsr = pM.getReportByIdUserAndIdPlan(user.getId_user(), id_plan, dateFrom, dateTo, "all");
						    		double realizationPlan = pM.getRealizationOfPlan(reportUsr.getNumberOfConfirms(), reportUsr.getNumberOfSurveys());
						    		ReportCost cost = pM.getTotalSurveyAndRecomendCostForUser(reportUsr.getNumberOfSurveys(), reportUsr.getNumberOfRecommend(), priceSurvey, priceRecommend);
						    		totalSurveyCost += cost.getSurveyCost();
						    		totalRecommendCost += cost.getRecommendCost();
						    		totalCost += cost.getTotalCost();
					%>
						<tr>
							<td><%=user.getFirstname() + " " + user.getLastname() %></td>
							<td><%= reportUsr.getNumberOfSurveys() %></td>
							<td><%= df.format(cost.getSurveyCost()) %>zł</td>
							<td><%= reportUsr.getNumberOfRecommend() %></td>
							<td><%= df.format(cost.getRecommendCost()) %>zł</td>
							<td style="width: 75px;"><b><%= df.format(cost.getTotalCost()) %>zł</b></td>
						</tr>
					<%} %>
				</tbody>
			</table>
		</div>
		<div style="width: 310px; display: inline-block; margin-left: 40px; max-height: 225px; overflow: auto; max-width: 310px;">
				<table id="surveyC" class="ui-widget ui-widget-content" style="width: 310px;" >
				<thead>
					<tr class="ui-widget-header ">
						<th colspan="3">Cennik</th>
					</tr>
					<tr class="ui-widget-header ">
						<th>Cena ankiety</th>
						<th colspan="2">Ilość ankiet (od - do)</th>
					</tr>
				<tbody>
					<%for(PriceListSurvey pSurvey : priceSurvey){ %>
						<tr>
							<td><%= df.format(pSurvey.getPrice1Survey()) %>zł</td>
							<td><%= pSurvey.getBegin() %></td>
							<td><%= pSurvey.getEnd() %></td>
						</tr>
					<%} %>
					<tr class="ui-widget-header ">
						<th>Cena polecenia</th>
						<th colspan="2">Ilość poleceń (od - do)</th>
					</tr>
					<%for(PriceListRecommend pRecommend : priceRecommend){ %>
						<tr>
							<td><%= df.format(pRecommend.getPrice1Recommend()) %>zł</td>
							<td><%= pRecommend.getBeginRecommend() %></td>
							<td><%= pRecommend.getEndRecommend() %></td>
						</tr>
					<%} %>		
				</tbody>	
			</table>
		</div>
		<div style="width: 460px; display: inline-block">
			<table id="surveys" class="ui-widget ui-widget-content">
				<thead>
					<tr class="ui-widget-header ">
						<th colspan="2">Ilość kontaktów (ankiety + polecenia)</th>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td>Ilość ankiet zebranych przez ankieterów</td>
						<td style="width: 45px;"><%=report.getNumberOfSurveys() %></td>
					</tr>
					<tr>
						<td>Ilość ankiet zebranych przez dział HR</td>
						<td><%=report.getNumberOfSurveysHR() %></td>
					</tr>
					<tr>
						<td><b>Łączna ilość ankiet</b></td>
						<td><b><%=report.getTotalSurveys() %></b></td>
					</tr>
					<tr>
						<td>Ilość poleceń zebranych przez ankieterów</td>
						<td><%=report.getNumberOfRecommend() %></td>
					</tr>
					<tr>
						<td>Ilość poleceń zebranych przez dział HR</td>
						<td><%=report.getNumberOfRecommendHr() %></td>
					</tr>
					<tr>
						<td><b>Łączna ilość poleceń</b></td>
						<td><b><%=report.getTotalRecommend() %></b></td>
					</tr>
					<tr style="background-color: #eaeaea; font-weight: bold;">
						<td >Razem</td>
						<td><%=report.getTotalSurveys() + report.getTotalRecommend() %></td>
					</tr>
				</tbody>
			</table>
		</div>
		<div style="width: 310px; display: inline-block; margin-left: 40px;">
		<% ReportCost realCost = pM.getSumaryAndSingleRealCostForSurveys(totalSurveyCost, totalRecommendCost, report.getTotalSurveys(), report.getTotalRecommend()); %>
			<table id="sumCost" class="ui-widget ui-widget-content">
				<thead>
					<tr class="ui-widget-header ">
						<th colspan="2">Koszty</th>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td>Koszt wszystkich ankiet</td>
						<td><%= df.format(totalSurveyCost) %>zł</td>
					</tr>
					<tr>
						<td>Koszt wszystkich poleceń</td>
						<td><%= df.format(totalRecommendCost) %>zł</td>
					</tr>
					<tr>
						<td><b>Razem</b></td>
						<td><b><%= df.format(totalCost) %>zł</b></td>
					</tr>
					<tr class="ui-widget-header ">
						<th colspan="2">Koszty jednostkowe</th>
					</tr>
					<tr>
						<td>Rzeczywisty koszt jednej ankiety</td>
						<td><%= df.format(realCost.getRealSingleSurveyCost()) %>zł</td>
					</tr>
					<tr>
						<td>Rzeczywisty koszt jednego polecenia</td>
						<td><%= df.format(realCost.getRealSingleRecommendCost()) %>zł</td>
					</tr>
					<tr>
						<td><b>Razem</b></td>
						<td><b><%= df.format(realCost.getRealSingleTotalCost()) %>zł</b></td>
					</tr>
				</tbody>
			</table>
		</div>
		
		
	</div>
</body>
</html>